package com.xiaofei.masterwork;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Master {
	private Queue<Object>workQueue = new ConcurrentLinkedQueue<>();
	private Map<String, Thread>threadMap = new HashMap<>();
	private Map<String,Object>resultMap = new ConcurrentHashMap<>();
	public Master(AbstractWorker abstractWorker,int count){
		for(int i=0;i<count;i++){
			abstractWorker.setResultMap(resultMap);
			abstractWorker.setWorkQueue(workQueue);
			threadMap.put(Integer.toString(i), new Thread(abstractWorker));
		}
	}
	public void addTask(Object input){
		workQueue.add(input);
	}
	public void submit(){
		for(Map.Entry<String, Thread> entry:threadMap.entrySet()){
			entry.getValue().start();
		}
	}
	public boolean isComplete(){
		for(Map.Entry<String, Thread> entry:threadMap.entrySet()){
			if(entry.getValue().getState()!=Thread.State.TERMINATED){
				return false;
			}
		}
		return true;
	}
	public Map<String, Object>getResultMap(){
		return this.resultMap;
	}
}
